import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import '../../../dd_core/dd_config.dart';
import '../../../dd_core/dd_log_service.dart';
import '../../../r.dart';
import '../controllers/dd_personal_center_controller.dart';
import '../views/dd_view_background.dart';
import 'dd_about_page.dart';
import 'dd_block_list_page.dart';
import 'dd_coin_shop_page.dart';
import 'dd_edit_profile_page.dart';
import 'dd_my_atical_page.dart';
import 'dd_my_follow_page.dart';
import 'dd_setting_page.dart';

class DDPerSonalCenterPage extends StatefulWidget {
  const DDPerSonalCenterPage({Key? key}) : super(key: key);

  @override
  State<DDPerSonalCenterPage> createState() => _DDPerSonalCenterPageState();
}

class _DDPerSonalCenterPageState extends State<DDPerSonalCenterPage> {
  DDPersonalCenterController _controller = DDPersonalCenterController();
  @override
  Widget build(BuildContext context) {
    return GetBuilder<DDPersonalCenterController>(
        init: _controller,
        initState: (state) {
          DDLogService().ddGa(page: "personal_center_page");
        },
        dispose: (state) {},
        builder: (controller) {
          return Scaffold(
            backgroundColor: Color(0xFFE7F6FC),
            body: SafeArea(
              top: true,
              child: SingleChildScrollView(
                child: Obx(
                  () => Column(
                    children: [
                      Stack(
                        children: [
                          Column(
                            children: [
                              Padding(
                                padding: EdgeInsets.only(
                                    top: 0.0, left: 0, right: 0),
                                child: Image.asset(
                                  R.assetsImagesPerson1,
                                  width: double.infinity,
                                  fit: BoxFit.fill,
                                ),
                              ),
                              SizedBox(
                                height: 44,
                              )
                            ],
                          ),
                          Positioned(
                            top: 0,
                            left: 16,
                            child: GestureDetector(
                                onTap: () {
                                  DDRoutesConfig.push(DDCoinShopPage(),
                                      routeName: "/doinShopPage");
                                },
                                child: DDViewBackGround(
                                  margin: EdgeInsets.only(top: 20, left: 20),
                                  content: Row(
                                    mainAxisAlignment: MainAxisAlignment.center,
                                    children: [
                                      SizedBox(width: 11),
                                      Padding(
                                        padding: EdgeInsets.only(
                                            top: 2,
                                            bottom: 2,
                                            left: 0,
                                            right: 0),
                                        child: Image.asset(
                                          R.assetsImagesPerson11,
                                          height: 22,
                                        ),
                                      ),
                                      SizedBox(width: 5),
                                      Text(
                                        '${_controller.availableCoins}',
                                        style: TextStyle(
                                            fontSize: 16,
                                            color: Colors.black,
                                            fontWeight: FontWeight.bold),
                                      ),
                                      SizedBox(width: 11),
                                    ],
                                  ),
                                )),
                          ),
                          Positioned(
                            bottom: 0,
                            left: MediaQuery.of(context).size.width / 2 - 50,
                            child:
                            Stack(children: [
                            ClipRRect(
                            borderRadius: BorderRadius.circular(43),
                            child: CachedNetworkImage(
                              width: 86,
                              height: 86,
                              fit: BoxFit.fill,
                              imageUrl: _controller.authModel.value.userinfo?.avatarUrl ?? '',
                              placeholder: (context, url) => Placeholder(),
                              errorWidget: (context, url, error) => Placeholder(),
                            ),
                          ),
                              // CachedNetworkImage(
                              //   width: 86,
                              //   height: 86,
                              //   fit: BoxFit.fill,
                              //   imageUrl: _controller
                              //       .authModel.value.userinfo?.avatarUrl ??
                              //       '',
                              //   placeholder: (context, url) => Placeholder(),
                              //   errorWidget: (context, url, error) =>
                              //       Placeholder(),
                              // ),
                              Positioned(
                                child: Container(
                                  width: 88,
                                  height: 88,
                                child:
                                Image.asset(
                                  R.assetsImagesPerson21,
                                  fit: BoxFit.fill,
                                  // fit: BoxFit.cover,
                                ),
                                ),
                            ),
                            ]),

                            // Container(
                            //   decoration: BoxDecoration(
                            //     borderRadius: BorderRadius.circular(44.0),
                            //     image: DecorationImage(
                            //       image: AssetImage(R.assetsImagesPerson21),
                            //       fit: BoxFit.cover,
                            //     ),
                            //   ),
                            //   child: CachedNetworkImage(
                            //     width: 88,
                            //     height: 88,
                            //     fit:BoxFit.fill,
                            //     imageUrl: _controller
                            //             .authModel.value.userinfo?.avatarUrl ??
                            //         '',
                            //     placeholder: (context, url) => Placeholder(),
                            //     errorWidget: (context, url, error) =>
                            //         Placeholder(),
                            //   ),
                            // ),
                          ),
                        ],
                      ),
                      Row(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [
                          Text(
                            _controller.authModel.value.userinfo?.nickname ??
                                '',
                            style: TextStyle(
                                fontSize: 18, fontWeight: FontWeight.bold),
                          ),
                          IconButton(
                            icon: Image.asset(R.assetsImagesPerson4),
                            onPressed: () {
                              DDRoutesConfig.push(DDEditProfilePage(),
                                  routeName: "/editProfilePage");
                            },
                          ),
                        ],
                      ),
                      Row(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [
                          Text(
                            'ID:${_controller.authModel.value.userinfo?.userId ?? ''}',
                            style: TextStyle(
                                fontSize: 16, color: Color(0x4D000000)),
                          ),
                          SizedBox(width: 5),
                          Container(
                            height: 14,
                            width: 32,
                            decoration: BoxDecoration(
                              border: Border.all(
                                color: Color(0xFF4C5961), // 边框颜色
                                width: 1.0, // 边框宽度
                              ),
                              borderRadius:
                                  BorderRadius.circular(4.0), // 边框圆角（可选）
                            ),
                            child: CupertinoButton(
                              borderRadius: BorderRadius.circular(4.0),
                              padding: EdgeInsets.only(
                                  top: 0, bottom: 0, left: 0, right: 0),
                              onPressed: () {
                                _controller.copyUserId(context);
                              },
                              child: Text(
                                'Copy',
                                style: TextStyle(
                                  //fontWeight: FontWeight.bold,
                                  fontSize: 10,
                                  color: Color(0x80000000),
                                ),
                              ),
                            ),
                          ),
                        ],
                      ),
                      SizedBox(
                        height: 22,
                      ),
                      MenuItem(
                        icon: R.assetsImagesPerson5,
                        title: 'My Articles',
                        onTap: () {
                          DDRoutesConfig.push(DDMyAticalPage(),
                              routeName: "/myAticalPage");
                        },
                      ),
                      MenuItem(
                        icon: R.assetsImagesPerson6,
                        title: 'My Follow',
                        onTap: () {
                          DDRoutesConfig.push(DDMyFollowPage(),
                              routeName: "/followPage");
                        },
                      ),
                      MenuItem(
                        icon: R.assetsImagesPerson7,
                        title: 'Block List',
                        onTap: () {
                          DDRoutesConfig.push(DDBlockListPage(),
                              routeName: "/blockListPage");
                        },
                      ),
                      MenuItem(
                        icon: R.assetsImagesPerson8,
                        title: 'About',
                        onTap: () {
                          DDRoutesConfig.push(DDAboutPage(),
                              routeName: "/aboutPage");
                        },
                      ),
                      MenuItem(
                        icon: R.assetsImagesPerson9,
                        title: 'Setting',
                        onTap: () {
                          DDRoutesConfig.push(DDSettingPage(),
                              routeName: "/settingPage");
                        },
                      ),
                    ],
                  ),
                ),
              ),
            ),
          );
        });
  }
}

class MenuItem extends StatelessWidget {
  final String icon;
  final String title;
  final VoidCallback onTap;

  MenuItem({
    required this.icon,
    required this.title,
    required this.onTap,
  });

  @override
  Widget build(BuildContext context) {
    return DDViewBackGround(
      margin: const EdgeInsets.only(top: 10, bottom: 10, left: 32, right: 32),
      content: ListTile(
        leading: Image.asset(icon),
        title: Text(
          title,
          style: TextStyle(fontSize: 16, color: Color(0xFF28401A)),
        ),
        trailing: Image.asset("assets/images/person10.png"),
        onTap: onTap,
      ),
    );
  }
}
